package com.proactive.webapp.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.proactive.webapp.dao.CourseCategory2DAO;
import com.proactive.webapp.dao.MenuDAO;
import com.proactive.webapp.dao.ResourceCategoryDAO;
import com.proactive.webapp.dao.ResourceDAO;
import com.proactive.webapp.entity.CourseCategory2;
import com.proactive.webapp.entity.Member;
import com.proactive.webapp.entity.Menu;
import com.proactive.webapp.entity.Resource;
import com.proactive.webapp.entity.ResourceCategory;
import com.proactive.webapp.util.Category;
import com.proactive.webapp.util.MenuUtil;
import com.proactive.webapp.util.PageCount;

@Controller
public class ResourceController {
	ApplicationContext context =  new ClassPathXmlApplicationContext("spring-module.xml");

	@RequestMapping(value = "/resource", method = RequestMethod.GET)
	public ModelAndView getResourceList(HttpServletRequest request, HttpSession session) {
		//import DAO
		ResourceCategoryDAO resourceCategoryDAO = (ResourceCategoryDAO) context.getBean("resourceCategoryDAO");
		ResourceDAO resourceDAO = (ResourceDAO) context.getBean("resourceDAO");
		ModelAndView model = new ModelAndView();
		

		//check login
		Member loginMember = (Member)session.getAttribute("loginMember");
		model.setViewName(loginMember!=null?"resource1":"resource2");
		String loginAccount = loginMember==null?"":loginMember.getAccount();
		
		//get data from request
		String type = request.getParameter("t")==null?"new":request.getParameter("t");
		String category1Req = request.getParameter("c1")==null?"":request.getParameter("c1");
		int pageNow = request.getParameter("p")==null?1:Integer.parseInt(request.getParameter("p"));
		
		//define id
		int category1ID = category1Req.equals("")?0:Integer.parseInt(category1Req);
		
		//set pager
		int pageSize = 16;
		int count = resourceDAO.countTotal(category1ID);
		String pageUrl = "resource";
		String pageEndUrl1 = "&t="+type+"&c1="+category1ID;
		PageCount pageCount = new PageCount(pageNow, pageSize, count, pageUrl, pageEndUrl1);
		pageCount.calculate();
		
		//set category
		String pageEndUrl2 = "&c1="+category1ID;
		Category category = new Category(type, category1ID, 0, pageUrl, pageEndUrl2);
		category.set2();
		
		//get resource by condition
		List<Resource> resourceList = new ArrayList<Resource>();
		List<ResourceCategory> category1List = new ArrayList<ResourceCategory>();
		category1List = resourceCategoryDAO.getList();
		if(category1ID != 0)
			resourceList = resourceDAO.getListByCategory(loginAccount, type, category1ID, pageCount.getStart(), pageSize);
		else
			resourceList = resourceDAO.getListByType(loginAccount, type, pageCount.getStart(), pageSize);

		//set category1 list(class,url,name...)
		ResourceCategory category1Index = new ResourceCategory();
		category1Index.setId(0);
		category1Index.setName("全部分類");
		category1Index.setClassString(category1ID==0?"selected":"");
		category1Index.setUrl(pageUrl+"?t="+category.getType());
		category1List.add(0, category1Index);
		for(int i=0; i<category1List.size(); i++){
			ResourceCategory category1 = category1List.get(i);
			if(category1.getId() == category1ID){
				category.setName1(category1.getName());
				category1List.get(i).setClassString("selected");
			}
			String url = pageUrl+"?t="+category.getType()+"&c1="+category1.getId();
			category1List.get(i).setUrl(url);
				
		}
		
		List<Menu> menuList = MenuUtil.getList(((MenuDAO)context.getBean("menuDAO")).getList(), 4);
		
		
		//set model
		model.addObject("page", pageCount);
		model.addObject("resourceList", resourceList);
		model.addObject("category1", category1List);
		model.addObject("category", category);
		model.addObject("menuList", menuList);
		

		
		return model;
	}
	
	@RequestMapping(value = "/addCount", method = RequestMethod.POST)
	public @ResponseBody void updateViewCount(int id) throws Exception {
		ResourceDAO resourceDAO = (ResourceDAO) context.getBean("resourceDAO");
		Resource resource = new Resource();
		resource.setId(id);
		resource = resourceDAO.get(resource);
		resource.setViewCount(resource.getViewCount()+1);
		resourceDAO.updateViewCount(resource);
	}
}
